package com.ychs.lawyer.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ychs.lawyer.annotation.RequiresPermission;
import com.ychs.lawyer.bean.R;
import com.ychs.lawyer.bean.User;
import com.ychs.lawyer.service.UserSevice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: clh
 * @Date: 2024/07/25/16:08
 * @Description:
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserSevice userSevice;

    /**
     * 根据条件查询用户
     * @param user
     * @return
     */

    @ApiOperation("根据条件获取所有用户")
    @GetMapping("/list")
    @RequiresPermission("systerm:user:list")
   public R<?> getUsersByCondition(User user){
        PageHelper.startPage(user.getPage(),user.getPageSize());
        List<User> users = userSevice.getUsersByCondition(user);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return R.success(pageInfo);
    }

    @ApiOperation("改变用户的启用状态")
    @PostMapping("/changeUserActive")
    public R<?> changeUserActive(@RequestBody User user) {
        int i = userSevice.changeUserActive(user);
        return i>0?R.success():R.fail();
    }
     /**
     * 添加
     */
     @ApiOperation("添加新用户")
     @PostMapping("/add")
     @RequiresPermission("systerm:user:add")
    public R<?> addUser(@RequestBody User user){
       int res = userSevice.addUser(user);
       return res > 0?R.success():R.fail();
    }

    /**
     * 修改
     */
    @ApiOperation("修改用户信息")
    @PostMapping("/edit")
    @RequiresPermission("systerm:user:edit")
    public R<?> editUser(@RequestBody User user){
       int res = userSevice.editUser(user);
       return res > 0?R.success():R.fail();
    }
     /**
     * 修改
     */
     @ApiOperation("修改个人信息")
     @PostMapping("/editinfo")
    public R<?> editUserinfo(@RequestBody User user){
       int res = userSevice.editUserinfo(user);
       return res > 0?R.success():R.fail();
    }
     /**
     * 删除
     *
     */
     @ApiOperation("删除用户")
     @PostMapping("/delete")
     @RequiresPermission("systerm:user:del")
    public R<?> deleteUserByIds(@RequestBody String[] delList) {
        log.info("phones{}:",delList);
        int res = userSevice.deleteUserByIds(delList);
        return res > 0?R.success():R.fail();
    }
    @ApiOperation("重置用户密码")
    @GetMapping("/reSetPassword/{userId}")
    public R<?> reSetPassword(@PathVariable Integer userId) {
        log.info("{}",userId);
        int res = userSevice.reSetPassword(userId);
        return res>0?R.success():R.fail();
    }
    @ApiOperation("通过用户名获取用户信息")
    @GetMapping("/getUserByName/{username}")
    public R<?> getUserByName(@PathVariable String username) {
        log.info("{}",username);
        User user = userSevice.getUserByName(username);
        return R.success(user);
    }


}

